package com.haoliang.spring.component.java.type.filter;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @Author zhaohaoliang
 * @Date 创建于 上午10:42 2019/3/10
 * @Description 组件自定义扫描过滤规则类
 */
public class CustomTypeFilter implements TypeFilter {
    // MetadataReader 读取到的当前正在扫描的类信息
    // MetadataReaderFactory 可以获取到其他任何类的信息
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        // 获取当前类的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 获取当前类的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 获取当前类的资源信息
        Resource resource = metadataReader.getResource();

        String className = classMetadata.getClassName();
        if (className.contains("Service")){
            return true;
        }
        return false;
    }
}
